const vm = new Vue({
    el: '#eformapp',
    data: {
        formJson: {"widgetList":[],"formConfig":{"labelWidth":80,"labelPosition":"left","size":"","labelAlign":"label-left-align","cssCode":"","customClass":"","functions":"","layoutType":"PC","onFormCreated":"","onFormMounted":"","onFormDataChange":"","onFormValidate":""}},
        formData: {},
        optionData: {},
        templateId: '',
        tempOneCode:'',
        dataLinkTable: '',
        iframeHeight:0,
        // 显示默认的提交按钮
        showDefaultSubmitButton: true
    },
    watch: {
        iframeHeight(newVal, oldVal) {
            window.parent.postMessage(
                {
                    vueAppHeight:newVal
                },
                "*"
            );
            // window.parent.setContentHeight(newVal);
        },
    },
    //https://www.yuque.com/variantdev/vform/pi1mff#mE6tm
    mounted() {
        let that = this;
        this.iframeHeight = document.body.scrollHeight;
        console.log('newVal-->>' +   this.iframeHeight)
        setInterval(this.valChange, 200);
        // 初始化表单配置和数据
        this.templateId = T.p('templateId');
        this.tempOneCode = T.p('tempOneCode');
        if ( this.templateId){
            $.ajax({
                async: false,
                url: T.webRoot + '/eform/eformtemplate/info/' + this.templateId,
                type: 'GET',
                success: function(res) {
                    let templateConfig = $.parseJSON(res.data.templateConfig);
                    that.$refs.vFormRef.setFormJson(templateConfig);
                    that.submitUrl = res.data.submitUrl;

                },
                error: function(err) {
                    console.log('err', err)
                }
            });

        }else if (this.tempOneCode){
            $.ajax({
                async: false,
                url: T.webRoot + '/eform/eformtemplate/infoByOneCode/' + this.tempOneCode,
                type: 'GET',
                success: function(res) {
                    let templateConfig = $.parseJSON(res.data.templateConfig);
                    that.$refs.vFormRef.setFormJson(templateConfig);
                    that.submitUrl = res.data.submitUrl;
                    that.templateId  = res.data.templateId;
                },
                error: function(err) {
                    console.log('err', err)
                }
            });
        }

    },
    methods: {
        valChange: function() {
            this.iframeHeight = document.body.scrollHeight;
        },
        // 表单提交
        defaultSubmitForm: function() {
            let that = this;
            this.$refs.vFormRef.getFormData().then( function(formData) {
                // Form Validation OK
                console.log('submitForm1', JSON.stringify(formData));
                let data = {};
                console.log('this.templateId-->>' + that.templateId)
                if (that.templateId){
                    data = {
                        'templateId': that.templateId,
                        'dataLinkTable': that.dataLinkTable,
                        'dataJson': JSON.stringify(formData),
                    };
                    $.ajax({
                        url: 'eform/eformdata/save',
                        type: 'post',
                        contentType: 'application/json',
                        data: JSON.stringify(data),
                        success: function(res) {
                            window.parent.postMessage(
                                {
                                    resKeyId:res.keyId
                                },
                                "*"
                            );
                            layer.msg(res.msg, {icon: 6});
                           // window.close();
                        },
                        error: function(err) {
                            layer.msg('网络异常，请稍后重试！', {icon: 5});
                        }
                    });
                }else {
                    debugger
                    layer.msg('数据异常1111，请稍后重试！', {icon: 5});
                }


            }).catch( function(error) {
                // Form Validation Failed
                alert(error)
            })
        }
    }
});